package com.xywy.common.util;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

/**
* Toast提示类
* Created by Joker on 14-6-6.
*/
public class ToastUtil {

   public static void showToast(Context context, String msg) {
       Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
   }

   public static void showToast(Context context, int msgResId) {
       Toast.makeText(context, msgResId, Toast.LENGTH_SHORT).show();
   }

   //当测试阶段时true
   private static final boolean isShow = true;
   /**
    * 测试用
    * 在正式投入市场：删
    *
    * @param context
    * @param msg
    */
   public static void showToastTest(Context context, String msg) {
       if (isShow) {
           Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
       }
   }
   
   static Toast result;
   
   public static void showToast(Context context, CharSequence text, int duration) 
   {
	   if(result == null)
	   result = new Toast(context);
	   
//	   result.cancel();
	   
       LinearLayout mLayout=new LinearLayout(context);
       TextView tv = new TextView(context);
       tv.setText(text);
       tv.setTextColor(Color.WHITE);
       tv.setGravity(Gravity.CENTER);
//       mLayout.setBackgroundResource(R.drawable.widget_toast_bg);
       mLayout.setBackgroundColor(0xff000000);
       LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
//       int w=context.getResources().getDisplayMetrics().widthPixels / 2;
//       int h=context.getResources().getDisplayMetrics().widthPixels / 10;
       params.leftMargin =dip2px(context, 10);
       params.rightMargin =dip2px(context, 10);
       params.topMargin =dip2px(context, 10);
       params.bottomMargin =dip2px(context, 10);
       mLayout.addView(tv, params);
       result.setView(mLayout);
       result.show();
       Handler handler = new Handler(){
       	public void handleMessage(android.os.Message msg) {};
       };
       handler.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				result.cancel();
			}
		}, duration*100);
   }
   
	/**
	 * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
	 */
	public static int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}
	
}
